package org.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.entity.QuestionInfoEntity;
import org.example.entity.RuleEntity;
import org.example.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.List;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rules")
public class RuleController {

    @Autowired
    private RuleService ruleService;

    /**
     * 分页查询规则
     */
    @GetMapping("/page")
    public IPage<RuleEntity> getRules(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return ruleService.getRules(pageNo, pageSize);
    }

    /**
     * 创建规则
     */
    @PostMapping("/createRule")
    public ResponseEntity<RuleEntity> createRule(@RequestBody RuleEntity rule) {
        RuleEntity createdRule = ruleService.createRule(rule);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdRule);
    }

    /**
     * 更新规则
     */
    @PutMapping("/updateRule/{id}")
    public ResponseEntity<RuleEntity> updateRule(@PathVariable Integer id, @RequestBody RuleEntity rule) {
        System.out.println("Received ID: " + id);
        System.out.println("Received Rule: " + rule);
        RuleEntity updatedRule = ruleService.updateRule(id, rule);
        return ResponseEntity.ok(updatedRule);
    }

    /**
     * 删除规则
     */
    @DeleteMapping("/deleteRule/{id}")
    public ResponseEntity<Void> deleteRule(@PathVariable Integer id) {
        ruleService.deleteRule(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据 ID 查询规则
     */
    @GetMapping("/getRule/{id}")
    public ResponseEntity<RuleEntity> getRule(@PathVariable Integer id) {
        RuleEntity rule = ruleService.getRule(Math.toIntExact(id));
        return ResponseEntity.ok(rule);
    }

    /**
     * 查询所有规则
     */
    @GetMapping("/getAll")
    public ResponseEntity<List<RuleEntity>> getAllRules() {
        List<RuleEntity> rules = ruleService.getAllRules();
        return ResponseEntity.ok(rules);
    }

    @PostMapping("/getExamPaper/{totalQuestions}")
    public List<QuestionInfoEntity> getExamPaper(@RequestBody RuleEntity rule, @PathVariable int totalQuestions) {
        return ruleService.getExamPaper(rule, totalQuestions);
    }

    @PostMapping("/generatePracticeBySubject")
    public List<QuestionInfoEntity> generatePracticePaperBySubject(
            @RequestParam String subject,
            @RequestParam int totalQuestions
    ) {
        return ruleService.generatePracticePaperBySubject(subject, totalQuestions);
    }

    @PostMapping("/generatePracticeByWrongQuestions")
    public List<QuestionInfoEntity> generatePracticePaperByWrongQuestions(
            @RequestParam int studentId,
            @RequestParam int totalQuestions
    ) {
        return ruleService.generatePracticePaperByWrongQuestions(studentId, totalQuestions);
    }


}
